package com.itxiuyixiu.service;

import com.itxiuyixiu.tools.qiniuyun.QiniuyunTemplateService;
import com.itxiuyixiu.tools.security.user.GetUser;
import com.itxiuyixiu.vo.QiniuyunVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author 黄磊
 * @date 2020/5/21
 */
@Service
public class QiniuyunService {
    @Autowired
    private QiniuyunTemplateService qiniuyunTemplateService;

    public QiniuyunVo getUploadPhotoToken() {
        String userName = GetUser.getUserName();
        UUID uuid = UUID.randomUUID();
        String key = userName + "-" + uuid.toString();
        String token = qiniuyunTemplateService.getUploadToken(key);
        return new QiniuyunVo(token, key);
    }

    /**
     * 删除文件
     *
     * @param key 文件的key
     * @return 删除状态（0-删除文件成功，1-文件不属于删除的人，2-文件删除失败，可能文件不存在）
     */
    public int deleteFile(String key) {
        if (!key.startsWith(GetUser.getUserName())) {
            return 1;
        }
        if (!qiniuyunTemplateService.deleteFile(key)) {
            return 2;
        }
        return 0;
    }
}
